package edu.hrbu.trade01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.hrbu.trade01.entity.RoleMenu;
import edu.hrbu.trade01.mapper.RoleMenuMapper;
import edu.hrbu.trade01.service.RoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 徐登宇
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    // 重新分配菜单
    @Transactional
    @Override
    public void toAssign(List<Long> menuIdList, Long roleId) {
        // 先删除曾经拥有的菜单
        LambdaQueryWrapper<RoleMenu> wrapperRoleMenu = new LambdaQueryWrapper<>();
        wrapperRoleMenu.eq(RoleMenu::getRoleId, roleId);
        remove(wrapperRoleMenu);
        // 再添加新分配的菜单
        for (Long menuId: menuIdList) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);
            save(roleMenu);
        }

    }
}
